package com.tac.service.mes.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tac.common.result.Result;
import com.tac.dao.mes.ProductionPlanDao;
import com.tac.pojo.mes.ProductionPlan;
import com.tac.service.mes.ProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductionPlanServiceImpl implements ProductionPlanService {

    @Autowired
    private ProductionPlanDao productionPlanDao;

    //查看生产计划
    public Result<PageInfo<ProductionPlan>> getList(Integer pageNum,Integer pageSize,
            String project, String planNumber, String name, Date deliveryDateStart, Date deliveryDateEnd, String applicant,
            Date applicantDateStart, Date applicantDateEnd, String applicantStatus, String auditor, Date examineDateStart,
            Date examineDateEnd, String examineStatus
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<ProductionPlan> list = productionPlanDao.getList(project, planNumber, name, deliveryDateStart, deliveryDateEnd, applicant, applicantDateStart, applicantDateEnd, applicantStatus, auditor, examineDateStart, examineDateEnd,examineStatus
        );
        PageInfo<ProductionPlan> productionPlanPageInfo = new PageInfo<>(list);
        return Result.success(productionPlanPageInfo);
    }
    //添加生产计划
    public Result save(ProductionPlan productionPlan){
        //待处理申请人先模拟一个
        productionPlan.setApplicant("童岸尘");
        if(!productionPlan.getApplicantStatus().equals("待审核")&&!productionPlan.getApplicantStatus().equals("未提交")){
            return Result.error("只能提交审核状态为未提交和待审核");
        }
        if(productionPlan.getApplicantStatus().equals("待审核")){
            //如果用户直接提交而不是保存，添加申请时间
            productionPlan.setApplicantDate(new Date());
        }
        Integer integer = productionPlanDao.save(productionPlan);
        if(integer<=0){
            return Result.error("添加生产计划失败");
        }
        return Result.success(null);
    }
    //删除生产计划
    public Result remove(List<Integer> ids){
        for(Integer id:ids){
            ProductionPlan pp = productionPlanDao.getById(id);
            if(!pp.getApplicantStatus().equals("未提交")){
                return Result.error("待审核或者已审核状态是无法删除的");
            }
        }
        Integer integer = productionPlanDao.remove(ids);
        if(integer<=0){
            return Result.error("删除生产计划失败");
        }
        return Result.success(null);
    }
    //更新生产计划
    public Result update(ProductionPlan productionPlan){
        //判断订单状态，如果是待审核或者已审核是无法修改生产计划的
        Integer id = productionPlan.getId();
        ProductionPlan pp = productionPlanDao.getById(id);
        if(productionPlan.getApplicantStatus().equals("已审核")){
            return Result.error("您没有权限，只能改为待审核");
        }
        if(!pp.getApplicantStatus().equals("未提交")){
            return Result.error("待审核或者已审核是无法修改生产计划的");
        }
        if (productionPlan.getApplicantStatus().equals("待审核")){
            //用户提交，添加申请时间
            productionPlan.setApplicantDate(new Date());
        }
        Integer integer = productionPlanDao.update(productionPlan);
        if(integer<=0){
            return Result.error("更新生产计划失败");
        }
        return Result.success(null);
    }
    //管理员修改审核信息
    public Result updateExamine(ProductionPlan productionPlan){
        Integer id = productionPlan.getId();
        ProductionPlan pp = productionPlanDao.getById(id);
        if(pp.getApplicantStatus().equals("未提交")){
            return Result.error("未提交状态不能审核");
        }
        //先模拟写一个审核人
        productionPlan.setAuditor("管理员");
        productionPlan.setExamineDate(new Date());
        if(!productionPlan.getExamineStatus().equals("通过")&&!productionPlan.getExamineStatus().equals("驳回")){
            return Result.error("状态为通过或者驳回");
        }
        Integer integer = productionPlanDao.updateExamine(productionPlan);
        if(integer<=0){
            return Result.error("生产计划审核失败");
        }
        return Result.success(null);
    }
}
